<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>销售流水</title>
		<link rel="stylesheet" href="__PEAR__/component/pear/css/pear.css" />
		<style>
		.layui-input{width:150px;}
		.layui-form-label{width:auto;}
		.layui-form-item .layui-input-inline{width: 150px;}
		.inputdiv{display:flex;height: 38px;line-height: 38px;}
		.inputdiv i{position:absolute;right:10px;}
		.layui-table-cell .layui-table-link {color: #ff6a08!important;}}
		</style>
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="" id="searchform">
					<div class="layui-form-item">
					
					<div class="layui-row">
						<div class="layui-form-item layui-inline">
								<label class="layui-form-label">分店仓库</label>
								<div class="layui-input-inline inputdiv">
									<input type="text" name="branch_no" id="branch_no" autocomplete="off" value="" placeholder="" class="layui-input" onfocus="showBranchs()">
									<i class="layui-icon layui-icon-home"></i>
								</div>
						</div>
						
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">商品货号</label>
							<div class="layui-input-inline inputdiv">
								<input type="text" name="item_no" id="item_no" placeholder="" autocomplete="off" class="layui-input" onfocus="selectGoods()">
								<i class="layui-icon layui-icon-cart-simple"></i>
							</div>
						</div>
						
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">自编码</label>
							<div class="layui-input-inline inputdiv">
								<input type="text" name="item_subno" id="item_subno" placeholder="" autocomplete="off" class="layui-input" onfocus="showPos()">
								<i class="layui-icon layui-icon-diamond"></i>
							</div>
						</div>
						
					</div>
					
					<div class="layui-row">
						
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">商品分类</label>
							<div class="layui-input-inline inputdiv">
								<input type="text" name="item_clsno" id="item_cls" placeholder="" autocomplete="off" class="layui-input" onfocus="selectCls()">
								<i class="layui-icon layui-icon-list"></i>
							</div>
						</div>
						
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">商品品牌</label>
							<div class="layui-input-inline inputdiv">
								<input type="text" name="item_brand" id="item_brand" placeholder="" autocomplete="off" class="layui-input" onfocus="selectBrand()">
								<i class="layui-icon layui-icon-vercode"></i>
							</div>
						</div>
						
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">供应商名</label>
							<div class="layui-input-inline inputdiv">
								<input type="text" name="supcust_no" id="supplier" placeholder="" autocomplete="off" class="layui-input" onfocus="showSupplier()">
								<i class="layui-icon layui-icon-username"></i>
							</div>
						</div>
					</div>
					
					<div class="layui-row">
					
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">汇总方式</label>
							<div class="layui-input-inline" style="width:630px;">
								<input type="radio" name="summary_type" lay-filter="summary_type" value="1" title="商品汇总" checked />
								<input type="radio" name="summary_type" lay-filter="summary_type" value="2" title="分类汇总" />
								<input type="radio" name="summary_type" lay-filter="summary_type" value="3" title="品牌汇总" />
								<input type="radio" name="summary_type" lay-filter="summary_type" value="4" title="商品汇总(不区分门店)" />
								<input type="radio" name="summary_type" lay-filter="summary_type" value="5" title="大类汇总" />
								<input type="hidden" id="summaryType" value="1" />
							</div>
						</div>
						
					</div>
					
					<div class="layui-row">
						<div class="layui-form-item layui-inline" id="date-range">
							<label class="layui-form-label">日期范围</label>
							<div class="layui-input-inline">
					          <input type="text" name="start" value="" autocomplete="off" id="start-date" class="layui-input" placeholder="开始日期">
					        </div>
					        <div class="layui-form-mid">-</div>
					        <div class="layui-input-inline">
					          <input type="text" name="end" value="" autocomplete="off" id="end-date" class="layui-input" placeholder="结束日期">
					        </div>
						</div>
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">快速设置</label>
							<div class="layui-input-inline">
								<select lay-filter="getdate">
									<option value="0">全部</option>
									<option value="1">今天</option>
									<option value="2">昨天</option>
									<option value="3">本周</option>
									<option value="4">上周</option>
									<option value="5">本月</option>
									<option value="6">上月</option>
									<option value="7">本季</option>
									<option value="8">上季</option>
									<option value="9">今年</option>
								</select>
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="summary-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md" onclick="resetForm()">
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
						</div>
					</div>
					
					</div>
					
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body" id="summary-body" style="min-height:500px;">
				<table id="summary-table" lay-filter="summary-table"></table>
			</div>
		</div>
		<script type="text/html" id="saleflowTpl">
  			<a onclick="showFlow('{{d.flow_no}}')" class="layui-table-link" target="_self">{{ d.flow_no }}</a>
		</script>
		<script src="__STATIC__/lib/layui/layui.js"></script>
		<script src="__PEAR__/component/pear/pear.js"></script>
		<script src="__STATIC__/js/admin/common.js"></script>
		<script>
			var branch_no="";
			var _layerIndex=null;
			var operator_url="{:U('Controls/operators')}";
			var supplier_url="{:U('Controls/suppliers')}";
			var pos_url="{:U('Controls/poslists')}";
			var items_url="{:U('Controls/items')}";
			var itemcls_url="{:U('Controls/itemcls')}";
			var brand_url="{:U('Controls/brands')}";
			var branchs_url="{:U('Controls/branchs')}";
			
			layui.use(['table', 'form', 'jquery','common','laydate'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let common = layui.common;
				let laydate = layui.laydate;
				
			  	laydate.render({
					    elem: '#date-range'
					    ,range: ['#start-date', '#end-date']
			    });

			  	form.on('select(getdate)', function(data) {
					date = new Array(2);
					var selc=parseInt(data.value);
		            switch (selc)
		            {
		            	case 1://当前天
	                    	date[0] = '{php}echo date('Y-m-d');{/php}';
	                    	date[1] = '{php}echo date('Y-m-d');{/php}';
	                    break;
		                case 2://昨天
		                    date[0] = '{php}echo date('Y-m-d', strtotime('-1 day'));{/php}';
		                    date[1] = '{php}echo date('Y-m-d', strtotime('-1 day'));{/php}';
		                    break;
		                case 3://本周
		                    date[0] = '{php}echo date('Y-m-d', mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));{/php}';
		                    date[1] = '{php}echo date('Y-m-d', mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));{/php}';
		                    break;
		                case 4://上周
		                    date[0] = '{php}echo date('Y-m-d', mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y")));{/php}';
		                    date[1] = '{php}echo date('Y-m-d', mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y")));{/php}';
		                    break;
		                case 5://本月
		                    date[0] = '{php}echo date('Y-m-d', mktime(0, 0 , 0,date("m"),1,date("Y")));{/php}';
		                    date[1] = '{php}echo date('Y-m-d', mktime(23,59,59,date("m"),date("t"),date("Y")));{/php}';
		                    break;
		                case 6://上月
		                    date[0] = '{php}echo date('Y-m-d', mktime(0, 0 , 0,date("m")-1,1,date("Y")));{/php}';
		                    date[1] = '{php}echo date('Y-m-d',mktime(23,59,59,date("m") ,0,date("Y")));{/php}';
		                    break;
		                case 7://本季度
		                    date[0] = '{php}echo date('Y-m-d', mktime(0, 0,0,ceil((date('n'))/3)*3-3+1,1,date('Y')));{/php}';
		                    date[1] = '{php}echo date('Y-m-d', mktime(23,59,59,ceil((date('n'))/3)*3,date('t',mktime(0, 0 ,0,ceil((date('n'))/3)*3,1,date("Y"))),date('Y')));{/php}';
		                    break;
		                case 8://上季度
		                    date[0] = '{php}echo date('Y-m-d', mktime(0, 0,0,(ceil((date('n'))/3)-1)*3-3+1,1,date('Y')));{/php}';
		                    date[1] = '{php}echo date('Y-m-d', mktime(23,59,59,(ceil((date('n'))/3)-1)*3,date('t',mktime(0, 0 ,0,(ceil((date('n'))/3)-1)*3,1,date("Y"))),date('Y')));{/php}';
		                    break;
		                case 9://本年
		                    date[0] = '{php}echo date('Y-01-01');{/php}';
		                    date[1] = '{php}echo date('Y-12-31');{/php}';
		                    break;
		            }
		            $('#start-date').val( date[0]);
	                $('#end-date').val(date[1]);
				});
				
			  	
				let DATA_PATH = "/{$MODULE_NAME}/pos/saleflow/";

				let sale_column1=[
						            {title: '销售数量',field: 'sale_qnty',align: 'left',width: 100},
						            {title: '销售金额',field: 'sale_money',align: 'left',width: 100},
						            {title: '退货数量',field: 'ret_qnty',align: 'left',width: 100},
						            {title: '退货金额',field: 'ret_money',align: 'left',width: 100},
						            {title: '赠送数量',field: 'giv_qnty',align: 'left',width: 100},
						            {title: '赠送金额',field: 'giv_money',align: 'left',width: 100},
						            {title: '让利金额',field: 'rl_money',align: 'left',width: 100},
						            {title: '数量小计',field: 'sum_qnty',align: 'left',width: 100},
						            {title: '金额小计',field: 'sum_money',align: 'left',width: 100}
						          ];
				
				let sale_column2=[
						            {title: '销售数量',field: 'sale_qnty',align: 'left',width: 100},
						            {title: '销售金额',field: 'sale_money',align: 'left',width: 100},
						            {title: '退货数量',field: 'ret_qnty',align: 'left',width: 100},
						            {title: '退货金额',field: 'ret_money',align: 'left',width: 100},
						            {title: '赠送数量',field: 'giv_qnty',align: 'left',width: 100},
						            {title: '赠送金额',field: 'giv_money',align: 'left',width: 100},
						            {title: '原销售金额',field: 'old_sale_money',align: 'left',width: 100},
						            {title: '让利金额',field: 'rl_money',align: 'left',width: 100},
						            {title: '数量小计',field: 'sum_qnty',align: 'left',width: 100},
						            {title: '金额小计',field: 'sum_money',align: 'left',width: 100}
						          ];
				let table_cols=[];

				//选择汇总分类后改变字段
				  window.changeField= function() {
				        var summaryType = $("#summaryType").val();
				        var columns = [];
				        columns.push({title: "行号", field: "rowIndex",align: 'left', width: 100});
				        
				        if (summaryType !== "4")
				        {
				            columns.push({title: "门店编号", field: "branch_no",align: 'left', width: 100});
				            columns.push({title: "门店编号", field: "branch_name",align: 'left', width: 120});
				        }
				        if (summaryType === "1" || summaryType === "4")
				        {
				            columns.push({title: "货号", field: "item_no",align: 'left', width: 120});
				            columns.push({title: "商品名称", field: "item_name",align: 'left', width: 150});
				            columns.push({title: "单位", field: "unit_no",align: 'left', width: 100});
				            columns.push({title: "规格", field: "item_size",align: 'left', width: 100});
				        }
				        if (summaryType !== "3")
				        {
				            columns.push({title: "分类编号", field: "item_clsno",align: 'left', width: 100});
				            columns.push({title: "分类名称", field: "item_clsname",align: 'left', width: 100});
				        }
				        if (summaryType === "1" || summaryType === "3" || summaryType === "4")
				        {
				            columns.push({title: "品牌编码", field: "item_brand",align: 'left', width: 100});
				            columns.push({title: "品牌名称", field: "brand_name",align: 'left', width: 100});
				        }
				        if (summaryType === "1")
				        {
				            columns.push({title: "原价", field: "unit_price",align: 'left', width: 100});
				            columns.push({title: "售价", field: "sale_price",align: 'left', width: 100});
				            columns.push({title: "折扣率", field: "zk",align: 'left', width: 100});
				        }
				        if (summaryType !== "1")
				        {
				            for (var i = 0; i < sale_column2.length; i++)
				            {
				                columns.push(sale_column2[i]);
				            }
				        }
				        else
				        {
				            for (var i = 0; i < sale_column1.length; i++)
				            {
				                columns.push(sale_column1[i]);
				            }
				        }
				        
				        table_cols[0]=columns;
			    }
				
				window.loadData=function(_where={}){
					
					changeField();
					
					table.render({
						elem: '#summary-table',
						url: DATA_PATH+"summary",
						page: true,
						cols: table_cols,
						height:600,
						where:_where,
						skin: 'line',
						toolbar: '',
						defaultToolbar: [],
						done:function(res, curr, count){
							var _width=$("#summary-body").width();
							$("#summary-table").css("width", _width);
						}
					});
				}
			  	
				//loadData();
			  	//汇总方式
				form.on('radio(summary_type)', function(data){
					  $("#summaryType").val(data.value);
				});  
				
				form.on('submit(summary-query)', function(data) {
					
					loadData(data.field);
					
					return false;
				});
				
				//显示分店选择框
				window.showBranchs=function(){
					_layerIndex=openWin("分店仓库选择",branchs_url,"800px","600px");
				}
				
				//门店选择后的回调函数
				window.callBackBranch=function(data){
					
					var arr=new Array();
					for(var i in data){
						arr.push(data[i]['branch_no']);
					}
					
					$("#branch_no").val(arr.join(","));
					
					if(_layerIndex!=null){
						layer.close(_layerIndex);
					}
					_layerIndex=null;
				}
				
				//显示操作员选择框
				window.showOperators=function(){
					var oval=$("#operator").val();
					
					if(_layerIndex==null||oval==''){
						_layerIndex=openWin("营业员选择",operator_url,"800px","600px");
					}
				}
				
				//操作员选择后的回调函数
				window.callBackOprator=function(data){
					
					$("#operator").val(data[0]['oper_id']);
					
					if(_layerIndex!=null){
						layer.close(_layerIndex);
						_layerIndex=null;
					}
				}
				
				//显示POS机选择框
				window.showPos=function(){
					var oval=$("#posid").val();
					
					if(_layerIndex==null||oval==''){
						_layerIndex=openWin("POS机选择",pos_url,"800px","600px");
					}
				}
				
				//POS机选择后的回调函数
				window.callBackPos=function(data){
					
					$("#posid").val(data[0]['posid']);
					
					if(_layerIndex!=null){
						layer.close(_layerIndex);
						_layerIndex=null;
					}
				}
				
				//选择商品窗口
				window.selectGoods=function(){
					_layerIndex=openWin("商品选择",items_url,"1100px","600px");
				}
				
				//商品选择后的回调函数
				window.callBack=function(data){
					
					$("#item_no").val(data[0]['item_no']);
					
					if(_layerIndex!=null){
						layer.close(_layerIndex);
					}
				}
				
				//选择商品分类窗口
				window.selectCls=function(){
					_layerIndex=openWin("商品分类选择",itemcls_url,"800px","600px");
				}
				
				//商品分类选择后的回调函数
				window.callBackItemcls=function(data){
					
					$("#item_cls").val(data[0]['item_clsno']);
					
					if(_layerIndex!=null){
						layer.close(_layerIndex);
					}
				}
				
				//选择商品品牌窗口
				window.selectBrand=function(){
					_layerIndex=openWin("商品品牌选择",brand_url,"800px","600px");
				}
				
				//商品品牌选择后的回调函数
				window.callBackBrand=function(data){
					
					$("#brand").val(data[0]['code_id']);
					
					if(_layerIndex!=null){
						layer.close(_layerIndex);
					}
				}
				
				//显示供应商选择框
				window.showSupplier=function(){
					_layerIndex=openWin("供应商选择",supplier_url,"800px","600px");
				}
				
				//供应商选择后的回调函数
				window.callBackSupplier=function(data){
					
					$("#supplier").val(data[0]['sp_no']);
					
					if(_layerIndex!=null){
						layer.close(_layerIndex);
						_layerIndex=null;
					}
				}

				//打开详细页面
				window.showFlow=function(flowno){
					openWin("销售流水详细",DATA_PATH+'saleDetail/flowno/'+flowno,"800px","500px");
				}
				
				window.refresh = function(param) {
					table.reload('summary-table');
				}
			})
		</script>
	</body>
</html>
